package juc.thread;


import java.util.concurrent.TimeUnit;

class DeadLockThread implements Runnable{
    private String lockA;
    private String lockB;

    public DeadLockThread(String lockA, String lockB) {
        this.lockA = lockA;
        this.lockB = lockB;
    }

    @Override
    public void run() {
        synchronized (lockA){
            System.out.println(Thread.currentThread().getName()+"\t 自己持有"+lockA+"\t 尝试获取"+lockB);
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (lockB){
                System.out.println(Thread.currentThread().getName()+"\t 自己持有"+lockB+"\t 尝试获取"+lockA);

            }
        }

    }
}

/**
 * @auther dyhan
 * @create 2020/3/13
 */
public class DeadLockDemo {
    public static void main(String[] args) {
        String lockA = "lockA";
        String lockB = "lockB";
        new Thread(new DeadLockThread(lockA,lockB),"A").start();
        new Thread(new DeadLockThread(lockB,lockA),"B").start();
    }
}
